# -*- coding: utf-8 -*-
import sys, xml.etree.ElementTree as ET
from xml.etree.ElementTree import ElementTree,Element,SubElement,parse 
# Create the root of the ElementTree from file
file_name="2.XML"
root_element="form"

# Loop using the iterator
# (Please use clear variable names!)

def indent(elem, level=0):
    i = "\n" + level*"  "
    if len(elem):
        if not elem.text or not elem.text.strip():
            elem.text = i + "  "
        if not elem.tail or not elem.tail.strip():
            elem.tail = i
        for elem in elem:
            indent(elem, level+1)
        if not elem.tail or not elem.tail.strip():
            elem.tail = i
    else:
        if level and (not elem.tail or not elem.tail.strip()):
            elem.tail = i

def bul(ara_tag,ara_text,yeni_text,root_element):
 # Create the root of the ElementTree from file
    try:
       # root = ET.parse(file_name).getroot()
        root = ET.parse(file_name).getroot()
# Get an iterator for the root node
        iterator = root.getiterator(root_element)   
    
        for item in iterator:
   # To find a sub-element with a text tag, use find()
            title_old_symbol = item.find(ara_tag)
      
    # This is how you get its text field
    
            text_title= title_old_symbol.text
            
    #print text_title,text_maximize,text_hidemouse,text_hideclock,text_hideinfopanel,text_infopanelalign,text_bgcolor
  
    # Is 'UPS' in our text field?
        if ara_text in text_title:
 
        # If so, remove the sub-element
        # remove() takes out nodes, not text
            item.remove(title_old_symbol)
 
        # Add a new sub-element to the item
            title_new_symbol = ET.SubElement(item, ara_tag)
            print title_new_symbol.text
        # Set its text field to the appropriate thing
        title_new_symbol.text = yeni_text 
        print title_new_symbol.text
        
        tree = ET.ElementTree(root)
        root=tree.getroot()
        SubElement(root, "one")
        #SubElement(root,"one","dd")
        indent(root)
        tree.write("out.xml")
        
    except: 
        print "BEşY OLUYO BANA!!".decode('UTF-8')

def find_text(element):
    if element.text is None:
        for subelement in element:
            for txt in find_text(subelement):
                yield txt
    else:
        yield element.text

#for txt in find_text(root): ustteki fonksiyonun kullanimi
 #   print txt
    
tag_temp=raw_input("tag lutfen \n")
if tag_temp=="0":
     sys.exit()
else : 
    print tag_temp,": \n"
    deger_temp = raw_input("aranan deger \n")
    print tag_temp,":",deger_temp ,"\n"

    deger_yeni = raw_input("Yeni deger \n")
    bul(tag_temp,deger_temp,deger_yeni,root_element) 




#bul("bgcolor","#FFFFFF","2") 
     
# Now get the full tree from the root


# And write to file!

